import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;

/*
 * Sample midlet to demonstrate the sending and receivng of messages
 * 
 * implements the characteristics of an information agent
 * 
 * Written by: Francis
 */
public class InfoTest extends MIDlet implements CommandListener,Runnable 
{
	
	private Form form;
	private Command cmExit,close;
	private TextField field;
	
    
    /** User interface command to exit the current application. */
    private final Command exitCommand = new Command("Exit", Command.EXIT, 2);

   
    
    /** User interface command to issue a request to send an animal by MMS. */
    private final Command mmsSendCommand = new Command("Send by MMS", Command.SCREEN, 1);
    
   

    /** User interface command to confirm current operation. */
    private final Command okCommand = new Command("OK", Command.OK, 3);

    /** User interface command to abort current operation. */
    private final Command cancelCommand = new Command("Cancel", Command.CANCEL, 1);
    
    /** User interface command to store the animal received by MMS. */
    private final Command Store = new Command("Show", Command.SCREEN, 1);

    
    static final Command selectCommand = new Command("Select" ,Command.SCREEN,3);
    static final Command backCommand = new Command("Back", Command.BACK, 1);
	
    /** The current display object. */
    private Display display;
    
    List type;
    
      
    /** Current command to process. */
    private Command currentCommand;

    /** The current command processing thread. */
    private Thread commandThread;
    
    /** The application-ID on which we send MMS messages */
    private String appID;

    /** Area where the user enters the phone number to send the message to */
    private TextField destinationField;

    /** Error message displayed when an invalid phone number is entered */
    private Alert errorMessageAlert;

    /** Alert that is displayed when a message is being sent */
    private Alert sendingMessageAlert;

    /** The screen used to prompt for the phone number to send the MMS to */
    private Form mmsSendScreen = null;
    
    /** The screen used to display the incoming MMS */
    private Form mmsReceiveScreen = null;
   
	
	List menuStuff;
			
	List Entry;
	
	Form info;
	Form mmsSendForm;
	public InfoTest() {
		// TODO Auto-generated constructor stub
		display = Display.getDisplay(this);
		
		
		cmExit= new Command("Exit",Command.EXIT,2);
		close= new Command("Close",Command.EXIT,2);
	
		//mms stuff
		
		errorMessageAlert = new Alert("MMS", null, null, AlertType.ERROR);
        errorMessageAlert.setTimeout(5000);

        sendingMessageAlert = new Alert("MMS", null, null, AlertType.INFO);
        sendingMessageAlert.setTimeout(1000);
        sendingMessageAlert.setCommandListener(this);
		
		form= new Form("Information Agent");
		form.setCommandListener(this);
		
	}
	
	/** 
     * Create MMS send form. 
     * Takes the message to send
     * and in destination phone number.
     * @return Form for the sending of MMS messages.
     */
	private Form createSendForm() {
		
		mmsSendForm = new Form("Information Agent");
		field= new TextField("Your Message", "",144,TextField.ANY);
		destinationField = new TextField("Destination Address: ", "mms://", 256, TextField.ANY);
		mmsSendForm.append(field);
		
		mmsSendForm.addCommand(cmExit);
		
		mmsSendForm.append(destinationField);
        mmsSendForm.addCommand(okCommand);
        
       
        mmsSendForm.setCommandListener(this);
        return mmsSendForm;
	}
	
	/** 
	 * Create MMS receive screen .
	 * Shows a "Receiving..." message during the act.
	 * @return Form for the receipt of MMS messages.
	 */
	private Form createReceiveForm() {
		 mmsReceiveScreen = new Form("Information Agent Receive");
        mmsReceiveScreen.addCommand(cancelCommand);
        mmsReceiveScreen.addCommand(Store);
        mmsReceiveScreen.setCommandListener(this);
        mmsReceiveScreen.append("Receiving.../n Message Received");
        return mmsReceiveScreen;
	}
	
	
	private void entryForm() {
		 Entry = new List("Information Agent",Choice.IMPLICIT);
		Entry.append("receiveByMMS", null);
		Entry.append("SendByMMS", null);
		
		Entry.addCommand(selectCommand);
		Entry.addCommand(close);
		Entry.setCommandListener(this);
		display.setCurrent(this.Entry);
	}
	
	private void makeAgentData()
	{
		/*System.out.println(destinationField.getString()+"    here.................");
		System.out.println("destina: "+ field.getString());
		*/
		MMSPlatform.sendByMMS2(
				this.destinationField.getString(), 
				field.getString(), 
				sendingMessageAlert, 
				errorMessageAlert, 
				appID, 
				display);
	}
	protected void destroyApp(boolean arg0)  {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		//start with a form that allow to choose]
		//between send or receive
		this.entryForm();
		
	}

public void commandAction(final Command c,final Displayable s){
	synchronized (this){
		// TODO Auto-generated method stub
		try{
		if ((s == this.Entry) && (c == selectCommand))
		{ 
		List down = (List)display.getCurrent();
         	switch(down.getSelectedIndex()) {
         		case 0: 
         			display.setCurrent(this.createReceiveForm());
         			//System.out.println("receiving 1 ");
         			break;
         		case 1: 
         		  display.setCurrent(this.createSendForm());
         		  break;
         	}
		}
		
		if ((c == cmExit)|| (c == close)) {
			
			destroyApp(false);
			notifyDestroyed();
		}
		
			
		if (
				(s == this.mmsSendForm) &&	(c == this.okCommand)){ 
			
//			System.out.println("destina: "+ field.getString());
		
		}
		if (c == this.mmsSendCommand){
			this.display.setCurrent(mmsSendScreen);
         }
		
		if ((s == mmsReceiveScreen) && (c == Store)){
    		
    				MMSPlatform.getFromMMS();
    				//System.out.println("receiving 2 ");
    		
	}
		}catch (Exception ex) {
            ex.printStackTrace();
        }
		 if (this.commandThread != null)
				// process only one command at a time
             return;

         this.currentCommand = c;
         this.commandThread = new Thread(this);
         this.commandThread.start();
     
	}
	}
	
	  /**
     * Perform the current command set by the method commandAction.
     */
    public void run() {
    	
        if (this.currentCommand == this.exitCommand) {
            this.destroyApp(false);
            this.notifyDestroyed();
        } 
		else if (this.currentCommand == this.Store){
			MMSPlatform.receiveMMS(appID,display,this.mmsReceiveScreen);
			//System.out.println("receiving>>>>>");
			this.display.setCurrent(this.mmsReceiveScreen);
		}
		else if (this.currentCommand == this.okCommand){
			
		this.makeAgentData();
		}
		

        synchronized (this) {
            // signal that another command can be processed
            this.commandThread = null;
        }
    }

}
